<?php
namespace MyAdmin\Model;

use Think\Model;

/**
 * TODO
 * 废弃
 */
class IndexModel extends Model
{
  protected $autoCheckFields =false;
  //登录
  public function login($phone, $passwd)
  {
    $rt_data = array('code'=>'success','msg'=>'123');

    $where["phone"] = $phone;
    #$where["status"] = 1;
    #$where["types"] = 2;
    $info = M("admins")->where($where)->find();

    if (!$info) {
      $rt_data['msg'] = '登陆失败，账号不存在或已关闭';
      $rt_data['code'] = 200021;
      return $rt_data;
    }

    $old_passwd = $info['passwd'];
    $salt = $info['salt'];
    $id = $info['id'];

    if (!$old_passwd) {
      $salt = random(6, 1);
      $o_pwd = 123456;
      $up_data['salt'] = $salt;
      $up_data['passwd'] = md5(md5($o_pwd).$salt);
      $status = M("admins")->where($where)->save($up_data);

      if ($status) {
        D("Admins")->set_admin_session($info);
        //日志？？

        $rt_data['msg']='登陆成功';

        //cookie登录
        //唯一标识生成
        $token = D("Admins")->create_token($info['id']);
        $_SESSION['token'] = $token;

        $value = $info['id']."|".$token;
        $value = encryption($value);
        setcookie('uautoadmin', $value, (C('AUTO_LOGIN_TIME')*3600*24+gmtime()), C('cookie_pe'));
      } else {
        $rt_data['msg'] = '登陆失败，密码有误！';
        $rt_data['code'] = 20002;
      }
    } elseif ($old_passwd==md5(md5($passwd).$salt)) {
      D("Admins")->set_admin_session($info);
      $rt_data['msg'] = '登陆成功';

      //cookie登录
      //唯一标识生成
      $token = D("Admins")->create_token($info['id']);

      $_SESSION['token'] = $token;
      $value = $info['id']."|".$token;
      $value = encryption($value);
      setcookie('uautoadmin', $value, (C('AUTO_LOGIN_TIME')*3600*24+gmtime()), C('cookie_pe'));
    } else {
      $rt_data['msg'] = '登陆失败，密码错误';
      $rt_data['code'] = 20002;
    }

    return $rt_data;
  }
}
